package com.hkjtgc.pcr_app.util

import android.annotation.SuppressLint
import android.net.ParseException
import java.sql.Time
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

internal object DateUtil {
    private const val dateFormat = "yyyy-MM-dd HH:mm:ss"
    private val threadLocal: ThreadLocal<DateFormat> = ThreadLocal<DateFormat>()

    @SuppressLint("SimpleDateFormat")
    private fun getDateFormat(): DateFormat {
        var df: DateFormat? = threadLocal.get()
        if (df == null) {
            df = SimpleDateFormat(dateFormat)
            threadLocal.set(df)
        }
        return df
    }

    @SuppressLint("SimpleDateFormat")
    fun getTestDate(test: String): String? {
        val short = "yyyy/MM/dd hh:mm:ss"
        val currentTime = getDateFormat().parse(test)
        println(currentTime)
        val formatter = SimpleDateFormat(short).format(currentTime)
        return formatter.format(currentTime)
    }

    fun convertLongToTime(time: Long): String {
        val date = Date(time)
        val format = SimpleDateFormat("yyyy/MM/dd hh:mm:ss")
        return format.format(date)
    }


    fun difference(start: Time, stop: Time): Time {
        val diff = Time(0, 0, 0)

        if (stop.seconds > start.seconds) {
            --start.minutes
            start.seconds += 60
        }

        diff.seconds = start.seconds - stop.seconds
        if (stop.minutes > start.minutes) {
            --start.hours
            start.minutes += 60
        }

        diff.minutes = start.minutes - stop.minutes
        diff.hours = start.hours - stop.hours

        return diff
    }

    /**
     * 计算2个时间相差的天数、小时、分钟、秒
     * @param startTime 开始时间
     * @param endTime 截止时间
     * @param format 时间格式 yyyy-MM-dd HH:mm:ss
     * @param str 返回的数据为：day-天、hour-小时、min-分钟、second-秒
     * @return
     */
    fun dateDiff(startTime: String?, endTime: String?, format: String?, str: String): Long {
        // 按照传入的格式生成一个simpledateformate对象
        val sd = SimpleDateFormat(format)
        val nd = (1000 * 24 * 60 * 60).toLong() // 一天的毫秒数
        val nh = (1000 * 60 * 60).toLong() // 一小时的毫秒数
        val nm = (1000 * 60).toLong() // 一分钟的毫秒数
        val ns: Long = 1000 // 一秒钟的毫秒数
        val diff: Long
        var day: Long = 0
        var hour: Long = 0
        var min: Long = 0
        var second: Long = 0
        // 获得两个时间的毫秒时间差异
        try {
            diff = sd.parse(endTime).time - sd.parse(startTime).time
            // 计算差多少天
            day = diff / nd
            // 计算差多少小时
            hour = diff / nh
            // 计算差多少分钟
            min = diff / nm
            // 计算差多少秒
            second = diff / ns
            // 输出结果
            println(
                "时间相差：" + day + "天" +
                        (hour - day * 24) + "小时"
                        + (min - day * 24 * 60) + "分钟" +
                        second + "秒。"
            )
            /*System.out.println("hour=" + hour + ",min=" + min);*/
        } catch (e: ParseException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        }
        if (str.equals("day", ignoreCase = true)) {
            return day
        } else if (str.equals("hour", ignoreCase = true)) {
            return hour
        } else return if (str.equals("min", ignoreCase = true)) {
            min
        } else {
            second
        }
    }

    /**
     * 返回的字符串形式是形如：2013-10-20 20:58
     */
    fun formatTimeInMillis(timeInMillis: Long): String {
        val cal = Calendar.getInstance()
        cal.timeInMillis = timeInMillis
        val date = cal.time
        val dateFormat = SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss"
        )
        return dateFormat.format(date)
    }

}
